<template>
  <div>
    <!-- 挂单记录 -->
    <el-table
      :data="state.tableData"
      class="customer-table"
      max-height="300"
      header-cell-class-name="eltablebg"
      style="width: 100%"
    >
      <el-table-column prop="" :label="$t('exchange.type')">
        <template #default="r">
          <span :class="r.row.type === 1 ? 'text-green' : 'text-red'">{{
            r.row.type === 1 ? $t("lever.buyup") : $t("lever.buyfall")
          }}</span>
        </template>
      </el-table-column>
      <el-table-column prop="" :label="$t('exchange.currentPrice')">
        {{ $currency(nowprice) }}
      </el-table-column>
      <el-table-column prop="deal_price" :label="$t('stock.orderAmount')">
        <template #default="r">
          {{ $currency(r.row.deal_price) }}
        </template>
      </el-table-column>
      <el-table-column
        align="center"
        prop="order_duration"
        :label="$t('lever.duration')"
      >
        <template #default="r"> {{ r.row.order_show_time }}s </template>
      </el-table-column>
      <el-table-column
        align="center"
        prop="open_time"
        :label="$t('lever.opentiming')"
      >
        <template #default="r">
          <span>{{ getTimeZoneDate(r.row.open_time) }}</span>
        </template>
      </el-table-column>
      <el-table-column
        align="center"
        prop="open_time"
        :label="$t('assets.handlingFee')"
      >
        <template #default="r">
          <span>{{ $currency(r.row.handling_fee) }}</span>
        </template>
      </el-table-column>
      <el-table-column align="right" prop="" :label="$t('assets.operate')">
        <template #default="r">
          <el-button type="text" @click="winloseStop(r.row.id)">{{
            $t("lever.cancelorder")
          }}</el-button>
        </template>
      </el-table-column>
      <template #empty>
        <div>
          <Norecord :useDark="true" />
          <!-- <div class="quotet3 text-12 mb-60 l-10">暂无数据</div> -->
        </div>
      </template>
    </el-table>

    <!-- 取消订单 -->
    <Messagebox
      :dialog="state.dialog"
      :text="$t('lever.confirmcancel')"
      :type="3"
      @onconfirm="confirm"
      @oncancer="state.dialog = false"
    />
    <!-- 监听table滚动条触底 -->
    <Tablescroll @scroll="tableScroll" />
  </div>
</template>

<script>
import Norecord from "@/components/noRecord/NoRecord.vue";
import { secondLeverPage, cancelSecondOrder } from "@/api/currency";
import { demosecondLeverPage, democancelSecondOrder } from "@/api/demo";
import UseSystemSetting from "@/commonapi/useSystemSetting";
import { onMounted, reactive, watch } from "vue";
import Messagebox from "@/components/messageBox/index.vue";
import Tablescroll from "@/components/tablescroll/index.vue";
import { LoginInterception } from "@/utils/common";
import { getTimeZoneDate } from "@/utils/times.js";
import {
  microtradeLists
} from "@/api/assets";
import { useStore } from "vuex";
import { reduceFix } from "@/utils/precisionMath";

export default {
  name: "OrderRecord",
  components: { Norecord, Messagebox, Tablescroll },
  props: { checked: Boolean, tradingid: Number, nowprice: Number },
  setup(props) {
    const state = reactive({
      listParam: {
        page: 1,
        limit: 100,
        order_status: 1,
      },
      loading: false,
      tableData: [],
      tradingid: props.tradingid,
      dialog: false,
      cancerid: 0,
      total: 0,
      checked: props.checked,
    });
    const $store = useStore();
    /**
     * methods
     */
    // 获取开关状态
    const { virtual_wallet } = UseSystemSetting();
    // eslint-disable-next-line no-unused-vars
    watch(
      () => props.tradingid,
      (val) => {
        if (val !== state.tradingid) {
          state.tradingid = val;
          state.listParam.page = 1;
          getList();
        }
      }
    );

    watch(
      () => props.checked,
      (val) => {
        console.log(val);
        state.listParam.page = 1;
        getList();
      }
    );

    const refresh = () => {
      state.listParam.page = 1;
      getList();
    };
    onMounted(() => {
      getList();
    });
    // 计算下单金额
    const CountDealPrice = (val) => {
      let price = 0;
      if (val.deal_price) {
        price = reduceFix(val.deal_price, val.handling_fee);
      }
      return price;
    };
    // 取消订单
    const winloseStop = (id) => {
      state.cancerid = id;
      state.dialog = true;
    };
    //  确认取消
    const confirm = () => {
      let setFun = virtual_wallet.value
        ? democancelSecondOrder
        : cancelSecondOrder;
      setFun(state.cancerid).then((res) => {
        if (res.code === 200) {
          state.listParam.page = 0;
          $store.commit("exchange/SET_REVOKEORDER", true);
          getList();
        }
      });
    };
    const tableScroll = () => {
      console.log("111", state.total, state.tableData.length);
      if (state.total > state.tableData.length) {
        state.listParam.page = state.listParam.page + 1;
        console.log(state.listParam.page);
        getList();
      }
    };

    const getList = () => {
      const param = { ...state.listParam };
      // 判断是否只显示当前币
      // if (props.checked) {

      // }
      console.log('挂单记录吗')
      param.currency_match_id = props.tradingid;
      state.loading = true;
      param.match_id=3
      param.status=3

      // let setFun = virtual_wallet.value
      //       ? demosecondLeverPage
      //       : secondLeverPage;
      let setFun = microtradeLists;
          setFun(param).then((res) => {
            console.log(res,'挂单记录')
            state.loading = false;
            state.tableData = res.data.data;
            // if (state.listParam.page === 0) {
            //   state.tableData = res.data.items;
            // } else {
            //   state.tableData = state.tableData.concat(res.data.items);
            // }
            state.total = res.data.total;
          });


      // LoginInterception("/secondlever")
      //   .then(() => {
      
      //   })
      //   .catch(() => {
      //     state.loading = false;
      //   });
    };
    return {
      state,
      getList,
      winloseStop,
      confirm,
      tableScroll,
      getTimeZoneDate,
      refresh,
      CountDealPrice,
    };
  },
};
</script>

<style scoped lang="less"></style>
